<template>
  <div>
    <Uploader action="/upload" 
    :boforeUpload="boforeUpload" 
    @file-uploaded="onFileUploaded"
    @file-uploaded-error="onfileUploadedError"></Uploader>
      <column-list :list="testData"></column-list>
  </div>
</template>

<script lang='ts'>
/* 使用上传文件 */
import {defineComponent,computed,onMounted} from 'vue'
import ColumnList from '@/components/ColumnList/ColumnList.vue'

import {useStore} from 'vuex'
import {GlobalDataProps,ResponseType,ImageProps} from '@/store/index'

import Uploader from '@/components/Uploader/Uploader.vue'

import createMessage from '@/hooks/createMessage'

export default defineComponent({
  components: {
    ColumnList,
    Uploader
  },
  setup(){
    // 为了能够更好的提示，我们这里使用泛型
    const store = useStore<GlobalDataProps>()

    onMounted(() => {
      // 经过此方式，把请求到的数据赋值给columns
      store.dispatch('fetchColumns')
    })
   // 然后
    const list = computed(()=>  store.state.columns)

    // 做上传前的一系列检查
    const boforeUpload = (file:File) => {
      const isPng = file.type == 'image/png'
      if (!isPng) {
        createMessage('上传文件格式不是png','error')
      }
      return isPng
    }

    // 上传成功
    const onFileUploaded = (rawData:ResponseType<ImageProps>) => {
      createMessage(`上传图片ID ${rawData.data._id}`,'success')
    }
    // 上传失败
    const onfileUploadedError = (err:any) => {
      console.log('err',err);
      const msg = err.message || '上传路径错误'
      createMessage(`上传文件 ${msg}`,'error')
      
    }
    return {
      testData:list,
      boforeUpload,
      onFileUploaded,
      onfileUploadedError
    }
  }
})
</script>

<style>

</style>